Dashboard Temp Share Shortlinks Frames API

HTMLify

Equalize the Towers.py
Views: 15 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def minCost(self, heights, cost):
        def get_cost(target_h):
            total = 0
            for h, c in zip(heights, cost):
                total += abs(h - target_h) * c
            return total
        low = min(heights)
        high = max(heights)
        ans = get_cost(low)
        while low <= high:
            mid1 = low + (high - low) // 3
            mid2 = high - (high - low) // 3
            
            cost1 = get_cost(mid1)
            cost2 = get_cost(mid2)
            
            ans = min(ans, cost1, cost2)
            
            if cost1 < cost2:
                high = mid2 - 1
            else:
                low = mid1 + 1
                
        return ans